<?php

namespace app\admin\controller;

use app\admin\model\Menu;
use app\admin\service\MenuService;
use app\admin\validate\MenuValidate;
use common\apidoc\Definitions;
use common\enum\ResultCode;
use hg\apidoc\annotation as Apidoc;
use support\Request;
use support\Response;
use support\Container;
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException;

#[Apidoc\Title("菜单管理")]
class MenuController extends BaseController
{
    private MenuService $service;
    protected MenuValidate $validate;

    public function __construct()
    {
        $this->service = Container::get(MenuService::class);
        $this->validate = Container::get(MenuValidate::class);
    }

    #[Apidoc\Title("菜单列表")]
    #[Apidoc\Url("admin/menu/index")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query(ref: [Definitions::class, "pagingQueryParam"])]
    #[Apidoc\Query("name", type: "string", desc: "菜单名称")]
    #[Apidoc\Query("status", type: "int", desc: "状态:0=隐藏,1=显示")]
    #[Apidoc\Returned(ref: [Definitions::class, "pagingReturnParam"])]
    #[Apidoc\Returned("list", type: "array", ref: Menu::class, desc: "菜单管理")]
    public function index(Request $request): Response
    {
        $params = [
            'page' => $request->get('page', 1),
            'limit' => $request->get('limit', 10),
            'name' => $request->get('name', ''),
            'status' => $request->get('status', ''),
        ];
        if (!$this->validate->check($params, 'index')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getList($params);
        return $this->success($result);
    }

    #[Apidoc\Title("菜单详情")]
    #[Apidoc\Url("admin/menu/info")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query("id", type: "int", require: true, desc: "菜单ID")]
    #[Apidoc\Returned(type: "object", ref: Menu::class, desc: "菜单详情")]
    public function info(Request $request): Response
    {
        $id = $request->get('id');
        if (!$this->validate->check(['id' => $id], 'info')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getInfo($id);
        return $this->success($result);
    }

    #[Apidoc\Title("添加菜单")]
    #[Apidoc\Url("admin/menu/create")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("parent_id", type: "int", default: 0, desc: "父级ID")]
    #[Apidoc\Param("name", type: "string", require: true, desc: "菜单名称")]
    #[Apidoc\Param("path", type: "string", desc: "路由路径")]
    #[Apidoc\Param("component", type: "string", desc: "组件路径")]
    #[Apidoc\Param("icon", type: "string", desc: "图标")]
    #[Apidoc\Param("permission", type: "string", desc: "权限标识")]
    #[Apidoc\Param("type", type: "string", require: true, desc: "类型:menu=菜单,button=按钮,api=接口")]
    #[Apidoc\Param("method", type: "string", desc: "请求方法")]
    #[Apidoc\Param("status", type: "int", default: 1, desc: "状态:0=隐藏,1=显示")]
    #[Apidoc\Param("sort", type: "int", default: 0, desc: "排序")]
    #[Apidoc\Returned(type: "object", ref: Menu::class, desc: "菜单详情")]
    public function create(Request $request): Response
    {
        $data = [
            'parent_id' => $request->post('parent_id', 0),
            'name' => $request->post('name'),
            'path' => $request->post('path'),
            'component' => $request->post('component'),
            'icon' => $request->post('icon'),
            'permission' => $request->post('permission'),
            'type' => $request->post('type'),
            'method' => $request->post('method'),
            'status' => $request->post('status', 1),
            'sort' => $request->post('sort', 0),
        ];
        if (!$this->validate->check($data, 'create')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->create($data);
        return $this->success($result, ResultCode::CREATE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("更新菜单")]
    #[Apidoc\Url("admin/menu/update")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("id", type: "int", require: true, desc: "菜单ID")]
    #[Apidoc\Param("parent_id", type: "int", default: 0, desc: "父级ID")]
    #[Apidoc\Param("name", type: "string", desc: "菜单名称")]
    #[Apidoc\Param("path", type: "string", desc: "路由路径")]
    #[Apidoc\Param("component", type: "string", desc: "组件路径")]
    #[Apidoc\Param("icon", type: "string", desc: "图标")]
    #[Apidoc\Param("permission", type: "string", desc: "权限标识")]
    #[Apidoc\Param("type", type: "string", desc: "类型:menu=菜单,button=按钮,api=接口")]
    #[Apidoc\Param("method", type: "string", desc: "请求方法")]
    #[Apidoc\Param("status", type: "int", default: 1, desc: "状态:0=隐藏,1=显示")]
    #[Apidoc\Param("sort", type: "int", default: 0, desc: "排序")]
    public function update(Request $request): Response
    {
        $id = $request->post('id');
        $data = [
            'parent_id' => $request->post('parent_id'),
            'name' => $request->post('name'),
            'path' => $request->post('path'),
            'component' => $request->post('component'),
            'icon' => $request->post('icon'),
            'permission' => $request->post('permission'),
            'type' => $request->post('type'),
            'method' => $request->post('method'),
            'status' => $request->post('status'),
            'sort' => $request->post('sort'),
        ];
        $data = array_filter(array_merge(['id' => $id], $data), function ($value) {
            return !empty($value) || $value === 0;
        });
        if (!$this->validate->check($data, 'update')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->update($id, $data);
        return $this->success($result, ResultCode::UPDATE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("删除菜单")]
    #[Apidoc\Url("admin/menu/delete")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("id", type: "int", require: true, desc: "菜单ID")]
    public function delete(Request $request): Response
    {
        $id = $request->post('id');
        if (!$this->validate->check(['id' => $id], 'delete')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $this->service->delete($id);
        return $this->success(null, ResultCode::DELETE_SUCCESS->getDescription());
    }
}